package com.cssnj.portal.web.user;
import com.cssnj.portal.service.user.LayoutService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.utility.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Map;

@Controller
@RequestMapping(value = "/user/layout")
public class LayoutController {

    @Autowired
    private LayoutService layoutService;

    @Autowired
    private Environment environment;

    @RequestMapping(value = {"/index.do"})
    public ModelAndView index(HttpServletRequest request) throws BaseException, SQLException ,JsonProcessingException {

        ModelAndView modelAndView =new ModelAndView("user/layout");
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());

        jsonRequest.setHandleCode("getUserSet");
        JsonResponse jsonResponse = layoutService.execute(jsonRequest);
        ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
        Map<String, Object> repData = jsonResponse.getRepData();
        modelAndView.getModelMap().put("zxData", objectMapper.writeValueAsString(repData));

        jsonRequest.setHandleCode("getAllMb");
        jsonResponse = layoutService.execute(jsonRequest);
        repData = jsonResponse.getRepData();
        modelAndView.getModelMap().put("mbList", objectMapper.writeValueAsString(repData.get("mbList")));

        jsonRequest.setHandleCode("getScList");
        jsonResponse = layoutService.execute(jsonRequest);
        repData = jsonResponse.getRepData();
        modelAndView.getModelMap().put("sjcGnList", objectMapper.writeValueAsString(repData.get("sjcGnList")));
        return modelAndView;

    }

    @ResponseBody
    @RequestMapping(value = {"/getUserSet.do"})
    public  JsonResponse getUserSet(HttpServletRequest request) throws BaseException, SQLException ,JsonProcessingException {
 ;      JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.setHandleCode("getUserSet");
        JsonResponse jsonResponse = layoutService.execute(jsonRequest);
        return jsonResponse;
    }

    /**
     * 保存展现类型和 图表的展现模板
     * @param request
      * @return
     * @throws BaseException
     * @throws SQLException
     */
    @ResponseBody
    @RequestMapping(value = {"/save.do"})
    public JsonResponse save(HttpServletRequest request,String zxlx,String mbid) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("zxlx",zxlx);
        jsonRequest.getReqData().put("mbid",mbid);
         jsonRequest.setHandleCode("save");
        JsonResponse jsonResponse = layoutService.execute(jsonRequest);
        return jsonResponse;
    }

    /**
     * 保存模板位置 对应的功能
     * @param request
      * @return
     * @throws BaseException
     * @throws SQLException
     */
    @ResponseBody
    @RequestMapping(value = {"/saveMbWz.do"})
    public JsonResponse saveMbWz(HttpServletRequest request,String wzid,String mbid,String tbyy_id) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("wzid",wzid);
        jsonRequest.getReqData().put("mbid",mbid);
        jsonRequest.getReqData().put("tbyy_id",tbyy_id);
        jsonRequest.setHandleCode("saveMbWz");
        JsonResponse jsonResponse = layoutService.execute(jsonRequest);
        return jsonResponse;
    }



   @RequestMapping(value = {"/showTbyy.do"})
    public ModelAndView showTbyy(HttpServletRequest request, String wzid,String mbid) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("wzid",wzid);
        jsonRequest.getReqData().put("mbid",mbid);
        jsonRequest.setHandleCode("showTbyy");
        JsonResponse jsonResponse = layoutService.execute(jsonRequest);
        ModelAndView modelAndView =new ModelAndView("user/showTbyy");
        return  modelAndView.addObject("tbyyList",jsonResponse.getRepData().get("tbyyList"));
    }




}
